#include "pcg.h"
#include <QDebug>

PCG::PCG(QObject *parent) :
    QObject(parent)
{
}

void PCG::addPoint(double x, double y)
{
    t << x;
    s << y;
}

QVector<double> *PCG::getX()
{
    return &t;
}

QVector<double> *PCG::getY()
{
    return &s;
}

void PCG::setBeginnings(QVector<int> &beg)
{
    beginnings.clear();
    for (int i = 0; i < beg.size(); i++)
    {
        beginnings << beg[i];
    }
}

int PCG::getMinimalPeriod()
{
    if (!beginnings.isEmpty())
    {
        int min = beginnings[beginnings.size() - 1];
        for (int i = 1; i < beginnings.size(); i++)
        {
            if ((beginnings[i] - beginnings[i - 1]) < min)
            {
                min = beginnings[i] - beginnings[i - 1];
            }
        }
        return min;
    }
    else
        return 0;
}

QVector<double> *PCG::getS1()
{
    if (!beginnings.isEmpty())
    {
        s1.empty();
        for (int i = 0; i < beginnings.size() - 1; i++)
        {
            int imin = beginnings[i];
            double min = s[imin];
            for (int j = beginnings[i]; (t[j] - t[beginnings[i]]) < 0.2; j++)
            {
                if (s[j] < min)
                {
                    min = s[j];
                    imin = j;
                }
            }
            s1 << t[imin] - t[beginnings[i]];
        }
        return &s1;
    }
    else
    {
        return NULL;
    }
}

QVector<double> *PCG::getS2()
{
    if (!beginnings.isEmpty())
    {
        s2.empty();
        for (int i = 0; i < beginnings.size() - 1; i++)
        {
            int imin = beginnings[i];
            double min = s[imin];
            for (int j = beginnings[i]; (t[j] - t[beginnings[i]]) < 0.6; j++)
            {
                if ((t[j] - t[beginnings[i]]) < 0.3)
                {
                    continue;
                }
                if (s[j] < min)
                {
                    min = s[j];
                    imin = j;
                }
            }
            s2 << t[imin] - t[beginnings[i]];
        }
        return &s2;
    }
    else
    {
        return NULL;
    }
}

int PCG::numberOfPeriods()
{
    return beginnings.size();
}
